package com.example.newsbackgroundmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.newsbackgroundmanagement.entity.BaseModel;
import com.example.newsbackgroundmanagement.entity.Likenews;
import com.example.newsbackgroundmanagement.mapper.ReleasenewsMapper;
import com.example.newsbackgroundmanagement.service.ILikenewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 邓旭波
 * @since 2023-03-26
 */
@Controller
@RequestMapping("/likenews")
public class LikenewsController {

    @Autowired
    ILikenewsService iLikenewsService;
    BaseModel baseModel;

    @Resource
    ReleasenewsMapper releasenewsMapper;


    @ResponseBody
    @RequestMapping(value = "/InsertLikeNews")//插入一条用户点赞的新闻
    public BaseModel InsertLikeNews(@RequestBody Likenews likenews){
        System.out.println("-----------进入LikenewsController的InsertLikeNews!");
        System.out.println("--------------------"+likenews);
        QueryWrapper<Likenews> likenewsQueryWrapper=new QueryWrapper<>();
        likenewsQueryWrapper.eq("luid",likenews.getLuid()).eq("lnid",likenews.getLnid());
        Likenews likenews1=iLikenewsService.getOne(likenewsQueryWrapper);
        if(likenews1==null) {
            likenews.setLikestate("true");
            System.out.println("++++++++++++++++++++++" + likenews);
            boolean flag = iLikenewsService.save(likenews);
            if (flag == true) {
                System.out.println("------插入成功");
                baseModel = new BaseModel(121, "插入用户点赞的新闻成功!");
                GetLikes(likenews);
            } else if (flag == false) {
                System.out.println("------插入失败");
                baseModel = new BaseModel(122, "插入用户点赞的新闻失败!");
            }
        }else{
            baseModel = new BaseModel(123, "该用户点赞的新闻已存在!");
        }
        return baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/SelectLikeState")//查询点赞的新闻的点赞状态
    public BaseModel SelectLikeState(@RequestBody Likenews likenews){
        System.out.println("-----------进入LikenewsController的SelectLikeState!");
        System.out.println("--------------------"+likenews);
        QueryWrapper<Likenews> likenewsQueryWrapper=new QueryWrapper<>();
        likenewsQueryWrapper.eq("luid",likenews.getLuid()).eq("lnid",likenews.getLnid());
        Likenews likenews1=iLikenewsService.getOne(likenewsQueryWrapper);
        if(likenews1!=null) {
                baseModel = new BaseModel(131, "插入用户点赞的新闻存在!");
                baseModel.setData(likenews1.getLikestate());
        }else{
            baseModel = new BaseModel(132, "该用户点赞的新闻不存在!");
        }
        return baseModel;
    }



    @ResponseBody
    @RequestMapping(value = "/SelectLikeNews")//查询所有点赞的新闻
    public BaseModel SelectLikeNews(){
        System.out.println("-----------进入LikenewsController的SelectLikeNews!");
        QueryWrapper<Likenews> likenewsQueryWrapper=new QueryWrapper<>();
        List <Likenews>likenews1=iLikenewsService.list(likenewsQueryWrapper);
        if(likenews1!=null) {
            baseModel = new BaseModel(141, "查找用户点赞的新闻成功!");
            baseModel.setDatas(likenews1);
        }else{
            baseModel = new BaseModel(142, "查找用户点赞的新闻不成功!");
        }
        return baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/DeleteLikeNews")//根据lnid和luid删除用户点赞的新闻
    public BaseModel DeleteLikeNews(@RequestBody Likenews likenews){
        System.out.println("-----------进入LikenewsController的DeleteLikeNews!");
        System.out.println("--------------------"+likenews);
        QueryWrapper<Likenews> likenewsQueryWrapper=new QueryWrapper<>();
        likenewsQueryWrapper.eq("lnid",likenews.getLnid()).eq("luid",likenews.getLuid());
        boolean bb=iLikenewsService.remove(likenewsQueryWrapper);
        if (bb==true){
            System.out.println("------删除成功");
            baseModel = new BaseModel(271, "删除用户点赞的新闻成功!");
            GetLikes(likenews);
        }else if (bb==false){
            System.out.println("------删除失败");
            baseModel = new BaseModel(272, "删除用户点赞的新闻失败!");
        }
        return baseModel;
    }

    @ResponseBody
    @RequestMapping(value = "/GetLikes")//获取用户发布新闻的总点赞数
    public void GetLikes(@RequestBody Likenews likenews){
        System.out.println("-----------进入LikenewsController的GetLikes!");
        System.out.println("--------------------"+likenews);
        QueryWrapper<Likenews> likenewsQueryWrapper=new QueryWrapper<>();
        likenewsQueryWrapper.eq("lnid",likenews.getLnid()).eq("likestate",likenews.getLikestate());
        long count=iLikenewsService.count(likenewsQueryWrapper);
        releasenewsMapper.SetLikesOnReleasenews(count,likenews.getLnid());
    }

}
